let app = getApp();
const previewUtil = require("./../../utils/preview.js");
import wxRequest from "../../utils/http.js"
import Notify from '/@vant/weapp/notify/notify';
import Toast from '@vant/weapp/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    allDynamic: [],
    allHeadPortrait: [],
    posts: [],
    imagesUrl: [],
    active: 0,
    show: 0,
    hidden: false,
    showCommentInput: false,
    showReplyCommentInput: false,
    dynamicId: '',//动态id
    commentContent: '',
    parentCommentId: '',//父级评论id
    parentCommenterId: '',// 父级评论者id
    parentCommenterNickName: '',// 父级评论者昵称
    replyCommentContent: '', // 回复输入内容
    replyCommentId: '', //回复父级评论id
    InputBottom: 0,
    TabCur: 0,
    scrollLeft: 0,
    commentTotal: '',
    likeTotal: ''
  },
  onChange(event) {
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '校园圈'
    })
    this.getDynamic();
  },
  onShow: function(){
    this.getDynamic();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getDynamic();
    wx.showToast({
      title: '刷新成功',
      icon: 'none',
      duration: 2000
    });
    wx.stopPullDownRefresh();
  },
  /**
   * 获取动态
   */
  async getDynamic() {
    wx.showLoading({
      title:"加载中"
    })
    let that = this;
    await wxRequest.get('/dynamic/selectAllDynamicList')
      .then(function (res) {
        that.setData({
          allDynamic: res.data.result
        })
        wx.hideLoading();
      })
  },
  changeData: function () {
    let that = this;
    that.onLoad();
  },
  /**
   * 写校园动态
   */
  writeDynamic() {
    wx.navigateTo({
      url: '../write_dynamic/write_dynamic'
    })
  },
  /**
   * 跳转到动态详情页面
   */
  toDynamicDetails(e) {
    wx.navigateTo({
      url: '../dynamic_details/dynamic_details?dynamicId=' + e.target.id
    })
  },
  /**
   * 预览单张图片
   * @param {*} event 
   */
  toPreviewImage(event) {
    previewUtil.previewImage(event);
  },
  /**
   * 预览多张图片
   * @param {*} event 
   */
  toPreviewMoreImage(event) {
    previewUtil.previewMoreImage(event);
  },
  /**
   * 将微信小程序的头像显示为高清
   * @param {*} event 
   */
  toPreviewWeChatAvatarUrlImage(event) {
    previewUtil.previewWeChatAvatarUrlImage(event);
  },
  /**
   * 点赞
   */
  praise: function (event) {
    this.setData({
      show: 0,
      hidden: false,
      showCommentInput: false
    });
  },

  /**
   * 显示评论控制面板
   */
  showComment: function (event) {

    this.setData({
      show: 0
    });
    let id = event.target.id;
    let hidden = event.target.dataset.show;
    console.log("动态id:" + id);
    if (!hidden) {
      this.setData({
        show: id,
        hidden: true
      });
    } else {
      this.setData({
        show: 0,
        hidden: false
      });
    }
  },
  /**
   * 触摸屏幕后移动触发一些隐藏操作
   */
  hiddenComment: function () {
    this.setData({
      show: 0,
      hidden: false,
      showCommentInput: false,
      showReplyCommentInput: false
    });
  },

  /**
   * 激活评论框
   */
  showCommentInput: function (event) {
    let objId = event.target.dataset.objid;
    this.setData({
      show: 0,
      hidden: false,
      showReplyCommentInput: false,
      showCommentInput: true,
      dynamicId: objId
    });
  },

  /**
   * 获取评论框的输入内容
   */
  getCommentContent: function (event) {
    let content = event.detail.value;
    this.setData({
      commentContent: ''
    });
    this.setData({
      commentContent: content
    })
  },
  getReplyCommentContent: function (e) {
    let replyContent = e.detail.value;
    this.setData({
      replyCommentContent: ''
    });
    this.setData({
      replyCommentContent: replyContent
    })
  },
 
  InputFocus(e) {
    this.setData({
      InputBottom: e.detail.height
    })
  },
  InputBlur(e) {
    this.setData({
      InputBottom: 0
    })
  },
  replyComment(event) {
    this.setData({
      dynamicId: ''
    });
    this.setData({
      dynamicId: event.currentTarget.dataset.dynamicid
    });
    // 评论id
    this.setData({
      replyCommentId: ''
    });
    this.setData({
      replyCommentId: event.currentTarget.id
    });
    this.setData({
      parentCommenterId: ''
    });
    this.setData({
      parentCommenterId: event.currentTarget.dataset.commenterid
    });
    this.setData({
      parentCommenterNickName: ''
    });
    this.setData({
      parentCommenterNickName: event.currentTarget.dataset.commenternickname
    })
  },
  /**
   * 展示回复评论框
   * @param event
   */
  showReplyCommentInput(event) {
    this.setData({
      show: 0,
      hidden: false,
      showCommentInput: false,
      showReplyCommentInput: true
    });
  },

})
